// pages/card/card.js
var utils = require('../../utils/util.js')
var app = getApp()
const domainUrl = app.globalData.domainUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msg:'init',
    paht:'',
    disabled: false,
    timer: '',//定时器名字
    countDownNum: '3600',//倒计时初始值
    countDownSecond: '0',
    nickName: '',
    picUrl: '',
    maxExpVal: '0',
    nowExpVal: '0',

    text: "本次更新内容：抽卡次数增加5次改成每小时10次，集卡册兑换需要消耗卡片，5张水浒卡兑换一张三国，不需要一样的，只要重复卡数大于3张就可以",
    marqueePace: 0.3,//滚动速度
    marqueeDistance: 0,//初始滚动距离
    marquee_margin: 30,
    size: 14,
    interval: 20 // 时间间隔
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    utils.login(this.route);
    this.setData({
      path:'./../card/card'
    });

    this.onShowExpLevel();
    this.onNews();

    var that = this;
    var openid = wx.getStorageSync('my_openid');

    setInterval(function () {
      wx.request({
        url: domainUrl + 'randomCardCountDown?openId=' + openid,
        success: function(res) {
          console.log(res.data.status);
          that.setData({
            disabled: res.data.status,
            countDownNum: res.data.countDown
          })
        }
      })
    }, 3000) //循环间隔 单位ms
  },

  selectCard: function () {
    var that = this;

    var openid = wx.getStorageSync('my_openid');
    console.log(openid);
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    wx.request({
      url: domainUrl+'randomCard?openId='+openid,
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        utils.showToast({
          context: that,
          title: res.data.data
        }),
        that.setData({
          disabled: true
        });
        that.onShowExpLevel();
      }
    })
  },
  showMore: function () {
    wx.navigateToMiniProgram({
      appId: 'wxabc8b1cc0308fca1',
    })
  },
  showMore1: function () {
    wx.navigateToMiniProgram({
      appId: 'wx52fb74b82e1e2234',
    })
  },
  showMore2: function () {
    wx.navigateTo({
      url: 'lottery/index',
    })
  },
  lottery: function () {
    wx.navigateTo({
      url: '../lottery/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onShowExpLevel();
    this.onNews();
  },
  onShowExpLevel() {
    var that = this;
    var openid = wx.getStorageSync('my_openid');

    wx.request({
      url: domainUrl + 'queryUserInfo?userId=' + openid,
      success: function (res) {

        console.log(res);
        console.log(res.data.data.gender);
        console.log(res.data.data.gender - res.data.data.expVal);
        console.log((res.data.data.gender - res.data.data.expVal) / res.data.data.language);

        that.setData({
          maxExpVal: res.data.data.gender,
          nowExpVal: res.data.data.expVal,
          expLevel: res.data.data.city,
          picUrl: res.data.data.picUrl,
          percent: (1-(res.data.data.gender - res.data.data.expVal) / res.data.data.language)*100
        })
      }
    })
  },
  onNews() {
    var that = this;
    wx.request({
      url: domainUrl + 'newsList',
      success: function (res) {
        console.log(res.data);
        that.setData({
          msgList: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '水浒卡',
      path: '/pages/card/card',
      success: function (res) { },
      fail: function (res) { }
    }
  }
})